import machine
from machine import Pin, PWM, ADC, I2C
import time
from rocker import ROCKER
from ssd1306 import SSD1306_I2C
# 超频到240Mhz
machine.freq(240000000)
rocker = ROCKER()  # 初始化摇杆
scr = SSD1306_I2C(128, 64, I2C(0, sda=Pin(4), scl=Pin(5)))
scr.fill(0)
scr.show()

init = True
pwm0 = PWM(Pin(17))
pwm0.freq(50)
pwm0.duty_ns(1000000)
time.sleep(2)
# if not init:
#     pwm0.duty_ns(2000000)
#     time.sleep(2.1)
#     pwm0.duty_ns(1000000)
#     time.sleep(1.1)

# for i in range(5, 101):
#     pwm0.duty_ns(1000000 + 10000 * i)
#     print(1000000 + 10000 * i)
#     time.sleep(0.1)
# pwm0.duty_ns(2000000)
# print(pwm0.duty_ns())
# time.sleep(10)
for i in range(0, 10000):
    scr.fill(0)
    scr.text("X : " + rocker.getX() + " %", 0, 12)
    pwm0.duty_ns(10000*int(float(rocker.getX())) + 1000000)
    scr.text("v:" + str(10000*int(float(rocker.getX())) + 1000000), 0, 24)
    scr.show()
    time.sleep_ms(50)
pwm0.deinit()
